<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>06_列表渲染</title>
</head>
<body>

<!--
1. 列表显示
  数组: v-for / index
  对象: v-for / key
2. 列表的更新显示
  删除item
  替换item
-->

<div id="demo">
  <h2>测试: v-for 遍历数组</h2>
  <ul>
    <li v-for="(p,index) in persons" :key="index">
      <!-- 此处还是vue数据绑定来渲染数据-->
      {{index}}--------{{p.name}}----------{{p.age}}
      ---<button @click="deleteP(index)">删除</button>
      ---<button @click="updateP(index,{name:'cat',age:20})">更新</button>
    </li>
  </ul>
  <h2>测试: v-for 遍历对象</h2>
  <ul>
    <li v-for="(value,key) in persons[1]">
      {{value}}---{{key}}
    </li>
  </ul>

</div>

<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
    new Vue({
      el: "#demo",
      data: {
        persons: [
          {name: 'Tom', age:18},
          {name: 'Jack', age:17},
          {name: 'Bob', age:19},
          {name: 'Mary', age:16}
        ]
      },
      methods: {
        deleteP(index){
          this.persons.splice(index,1);// 调用了不是原生数组的splice(), 而是一个变异(重写)方法
        },
        updateP(index,newP){
          this.persons.splice(index,1,newP);
        }
      },
    })
</script>
</body>
</html>